<?php
/**
 * 成本价格基础（通用）
 * (model)
 */

namespace App\Models\MonthlyReportCommon\Cost;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;
class PriceBasics extends Model
{
    protected $table = 'mr_cost_price_basics';

    protected $guarded  = [];

    public static function getPrice($pSkuId,$date){
        $re = self::query()->where('psku_id',$pSkuId)->where('b_date',$date)->first();
        if($re && $re->price){
            return $re->price;
        }else{
            $re = self::query()->where('psku_id',$pSkuId)->where('b_date','<',$date)->orderBy('b_date','DESC')->first();
            if($re && $re->price){
                return $re->price;
            }else{
                $ms = PriceBalance::query()
                    ->where('psku_id',$pSkuId)
                    ->where('storage_at','<',$date.' 00:00:00')
                    ->orderBy('storage_at','DESC')
                    ->first();
                if($ms && $ms->b_price){
                    return $ms->b_price;
                }else{
                    $p = ProductSku::query()->find($pSkuId);
                    return ProductSku::getPuPrice($p);
                }
            }
        }
    }
}
